package com.xtt.htmlunit;

import java.io.IOException;
import java.net.MalformedURLException;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlListItem;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class HtmlUnitTest {
	public void getPage() {
		@SuppressWarnings("resource")
		WebClient client = new WebClient(BrowserVersion.CHROME);//指定浏览器
		try {
			HtmlPage page = client.getPage("http://www.java1234.com");
			System.out.println(page.asXml());
			HtmlDivision div = page.getHtmlElementById("navMenu");
			System.out.println(div.asXml());
			HtmlListItem item = (HtmlListItem) page.getByXPath("//div[@id='navMenu'][1]/ul/ul").get(0);//xpath方式
			System.out.println(item.asXml());
		} catch (FailingHttpStatusCodeException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			client.close();
		}
	}
	
	public static void main(String args[]) {
		HtmlUnitTest htmlUnit = new HtmlUnitTest();
		htmlUnit.getPage();
	}
}
